package cc.iotkit.plugins.emqx.web;

import cc.iotkit.common.utils.ThreadUtil;
import cc.iotkit.plugins.emqx.service.EmqxPlugin;
import io.netty.handler.codec.mqtt.MqttQoS;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.vertx.core.Vertx;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.json.JsonObject;
import io.vertx.mqtt.MqttClient;
import io.vertx.mqtt.MqttClientOptions;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.configurationprocessor.json.JSONException;
import org.springframework.boot.configurationprocessor.json.JSONObject;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@Slf4j
@RestController
@RequestMapping("/atc")
@Api(tags = "MQ发送消息")
public class AtcController {
  @Autowired
  private EmqxPlugin emqxPlugin;

    @ApiOperation("开灯")
    @PostMapping("/login")
        private void reply(String topic, int duty, int pa0) throws JSONException {

        // 创建一个JsonObject实例
        JsonObject jsonObject = new JsonObject();

        // 组装device字段
        jsonObject.put("device", "C20241202244");

        // 创建一个JsonObject来表示data字段的内容
        JsonObject dataObject = new JsonObject();
        dataObject.put("id", 1); // 注意：Vert.x的JsonObject会自动处理数字的类型转换
        dataObject.put("method", "unicast");

        // 创建一个JsonObject来表示params字段的内容
        JsonObject paramsObject = new JsonObject();
        paramsObject.put("cmd", "set");

        // 创建一个JsonObject来表示aia字段的内容
        JsonObject aiaObject = new JsonObject();
        aiaObject.put("await", 0);
        paramsObject.put("aia", aiaObject);

        // 创建一个JsonObject来表示args字段的内容
        JsonObject argsObject = new JsonObject();

        // 创建一个JsonObject来表示dp字段的内容
        JsonObject dpObject = new JsonObject();
        dpObject.put("pa0", pa0);
        dpObject.put("duty0", duty);
        argsObject.put("dp", dpObject);

        // 将argsObject放入paramsObject
        paramsObject.put("args", argsObject);

        // 将paramsObject放入dataObject
        dataObject.put("params", paramsObject);

        // 将dataObject放入jsonObject
        jsonObject.put("data", dataObject);

        String finalTopic = "v5/10BBF3D4922A/gateway/rpc";

          emqxPlugin.replyT(finalTopic,jsonObject,0);

    }
}
